package cn.lk1234.imt.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.lk1234.imt.bean.UserInfo;
import cn.lk1234.imt.dao.dbtool.MapBean;
import cn.lk1234.imt.service.UserService;

@Controller
public class UserController {
	@Autowired
	public UserService userService;

	/**
	 * 访问用户列表页
	 */
	@RequestMapping(value = "/toUserPage")
	public ModelAndView toUserPage() throws IOException, SQLException {
		ModelAndView view = null;
		view = new ModelAndView("user/userList");
		return view;
	}
	
	/**
	 * 点击查询按钮（带条件分页查询用户列表）
	 */
	@RequestMapping(value = "/getUserList")
	public ModelAndView getUserList(Model model, UserInfo user) throws IOException, SQLException {
		ModelAndView view = null;
		//用于查询条件回显
		model.addAttribute("userReq", user);
		List<MapBean> userList = userService.getUserList(user);
		model.addAttribute("userList", userList);
		view = new ModelAndView("user/userList");
		return view;
	}
	
	/**
	 * 访问添加用户页
	 */
	@RequestMapping(value = "/toAddUserPage")
	public ModelAndView toAddUserPage() throws IOException, SQLException {
		ModelAndView view = null;
		
		view = new ModelAndView("user/userAdd");
		return view;
	}
	
	/**
	 * 点击添加按钮
	 */
	@RequestMapping(value = "/addUser")
	public @ResponseBody String addUser(UserInfo user) throws IOException, SQLException {
		
		String result = "OK";
		try { 
			int r = userService.addUser(user);
			if (r > 0) {
				result = "OK";
			} else {
				result = "ERROR";
			}
		} catch (Exception e) {
			result = "ERROR:" + e.getMessage();
		}
		return result;
	}
	
	/**
	 * 查看详情 
	 * 根据id获取详细信息
	 */
	@RequestMapping(value = "/getUserById")
	public @ResponseBody UserInfo getUserById(HttpServletRequest request,
			HttpServletResponse response,UserInfo user) throws SQLException {
		
		List<UserInfo> userList = userService.getUserById(user);
		request.getParameter("userNo");
		if(userList.size() > 0){
			return userList.get(0);
		}
		
		return null;
	}
	
	/**
	 * 访问编辑用户页
	 */
	@RequestMapping(value = "/toEditUserPage")
	public ModelAndView toEditUserPage() throws IOException, SQLException {
		ModelAndView view = null;
		
		view = new ModelAndView("user/userEdit");
		return view;
	}
	
	/**
	 * 点击编辑按钮
	 */
	@RequestMapping(value = "/editUser")
	public String editUser() throws IOException, SQLException {
		return null;
	}
	
	/**
	 * 点击删除按钮
	 */
	@RequestMapping(value = "/delUser")
	public String delUser() throws IOException, SQLException {
		return null;
	}
	
}
